//订单
//待付款 paymentState = PendingPay
//orderProcess
//等待发货 Waiting_for_delivery  Waiting_for_delivery
//等待收货 Already_shipped Success
//订单完成 Complete Complete  Success
//evaluation_state 1已评价 0未评价

//'orderState':'Complete','paymentState': 'Complete','evaluationState':'0'


$(function() {

//	objshowFn('.O_list', 0);

	$(".O_nav li").on('tap', function() {

		//		console.log($(this).index())
		objcssFn(".O_nav li", 'active', $(this).index());
		objshowFn('.O_listbox', $(this).index())

		//		基础信息
		var defaultStr = "'sorting':'generate_time','tosort':'asc'";
		var thisjson = '{' + defaultStr + ',' + $(this).attr("data-json") + '}';

		MuiAjaxFn("client/orderinfo/select?page=1&pagesize=11&adcode=100000", thisjson, function(data) {
			//查询订单信息
			if(data.pageInfo.size != 0) {
				//				console.log(data);
				var str = ' '
			
				$.each(data.pageInfo.list, function(idx, obj) {
					var orderInfo = obj.orderInfo;
					str += '<div class="O_list" data-orderNumber="' + orderInfo.orderNumber + '">';
					$.each(obj.orderInfoSelectCommdityVos, function(cidx, cobj) {
						//可能有多件商品
						var orderC = cobj.orderCommodities;
						var SalesC = cobj.bizCommodityForSales[0];
						var SalePrices = cobj.bizCommodityForSalePrices[0];
						var presentPrice = (orderC.commodityPrice / 100).toFixed(2);
						str += '<div class="O_commodity mui-row">' +
							'<div class="mui-col-xs-3 pimg-3left"><img src="' + IMGUrl + '' + SalesC.mainPhoto + '" width="100%"></div>' +
							'<div class="mui-col-xs-7 pcont-3center">' +
							'<p class="p2hide">' + SalesC.title + '</p><div class="psign">我的业务员</div></div>' +
							'<div class="mui-col-xs-2 pprice-3right">' +
							'<p >￥' + presentPrice + '</p><p>x' + orderC.buyAmount + '</p></div></div>';
					});
					str += Ordermenu(orderInfo.orderState, orderInfo.paymentState, orderInfo.evaluationState);
					str += '</div>';
				});
				//			str += ''
				//				if($(".O_listbox .O_list").length != 0) {
				$(".O_listbox:eq(" + $('.O_nav .active').index() + ")").html(str);
				menuTap();
				//				}
			} else {
				nodata('还没有该订单.', ".O_listbox:eq(" + $('.O_nav .active').index() + ")");
			}
		})
	});

if(GetQueryString("orderState")){
	$(".O_nav li:eq("+GetQueryString("orderState")+")").trigger("tap");
}else{
	$(".O_nav li:eq(0)").trigger("tap");
}
})

function Ordermenu(orderState, paymentState, evaluationState) {
	console.log(orderState + '  ' + paymentState)
	if(paymentState == 'PendingPay') {
		return '<div class="O_state"><span>待付款</span></div>' +
				'<div class="O_Viewstate bottomHr">' +
				'<button class="Cancelbtn" >取消订单</button>' +
				'<button class="RSubmitbtn">立即付款</button>' +
				'<button class="seebtn">查看订单</button>' +
				'</div>';

	}else if(paymentState == 'Unfinished') {
		if(orderState == 'Cancel_Order') {
			//取消订单
			return '<div class="O_state"><span>订单已失效</span></div>' ;
				
		}
	} else if(paymentState == 'Success') {
		//付款成功后
		if(orderState == 'Waiting_for_delivery') {
			//等待发货
			return '<div class="O_state"><span>待发货</span></div>' +
				'<div class="O_Viewstate bottomHr">' +
				'<button class="remindCom">提醒发货</button>' +
				'<button class="seebtn">查看订单</button>' +
				'</div>';
		} else if(orderState == 'Already_shipped') {
			//待收货
			return '<div class="O_state"><span>待收货</span></div>' +
				'<div class="O_Viewstate bottomHr">' +
				'<button class="completebtn">确认收货</button>' +
				'<button class="seebtn">查看订单</button>' +
				'</div>';
		} else if(orderState == 'waitAudit') {
			//待收货
			return '<div class="O_state"><span>等待审核中</span></div>' ;
				
		}else if(orderState == 'Complete'){
			if(evaluationState == 0) {
				return '<div class="O_state"><span>待评价</span></div>' +
					'<div class="O_Viewstate bottomHr">' +
					'<button class="nowEvaluate">立即评价</button>' +
					'<button class="seebtn">查看订单</button>' +
					'</div>';
			} else {
				return '<div class="O_state"><span>已完成</span></div>' +
					'<div class="O_Viewstate bottomHr">' +
					'<button class="seebtn">查看订单</button>' +
					'</div>';
			}
		}
		
	} 
//	else if(paymentState == 'Complete' && orderState == 'Complete') {
//		//订单完成
//		if(evaluationState == 0) {
//			return '<div class="O_state"><span>待评价</span></div>' +
//				'<div class="O_Viewstate bottomHr">' +
//				'<button class="nowEvaluate">立即评价</button>' +
//				'<button class="seebtn">查看订单</button>' +
//				'</div>';
//		} else {
//			return '<div class="O_state"><span>已完成</span></div>' +
//				'<div class="O_Viewstate bottomHr">' +
//				'<button class="seebtn">查看订单</button>' +
//				'</div>';
//		}
//
//	}
	else if(paymentState == 'Refund_Error') {
		return '<div class="O_state"><span>退款 失败</span></div>'+
		'<div class="O_Viewstate bottomHr">' +
				'<button class="seebtn">查看订单</button>' +
				'</div>';

	} else if(paymentState == 'Refund_Success') {
		return '<div class="O_state"><span>退款 失败</span></div>'+
		'<div class="O_Viewstate bottomHr">' +
				'<button class="seebtn">查看订单</button>' +
				'</div>';
	}

	//待付款 paymentState = PendingPay
	//orderProcess
	//等待发货 Waiting_for_delivery  Success 
	//等待收货 Already_shipped Success
	//订单完成 Complete Complete
	//evaluation_state 1已评价 0未评价
}


function myclick(v) {
	var llis = document.getElementsByTagName("li");
	for(var i = 0; i < llis.length; i++) {
		llis[i].className = '';
	}
	llis[v].className = 'active';
}


function menuTap() {
	$(".RSubmitbtn").on("tap", function() {
		RSubmitOrder($(this).parent().parent().attr("data-orderNumber"))
		
		
	})
	$(".completebtn").on("tap", function() {
		CompleteOrder($(this).parent().parent().attr("data-orderNumber"));
		window.location.reload()
		
		
			//http://hsmytest.vrdete.com/client/orderinfo/ordercomplete 确认收货
	})
	
	//提醒发货
	$(".remindCom").on("tap",function(){
		mui.toast("已提醒卖家尽快发货！")
	})
	
	//查看订单
	$(".seebtn").on("tap",function(){
		seeOrder($(this).parent().parent().attr("data-orderNumber"))
	})
	$(".nowEvaluate").on("tap",function(){
		EvaluateOrder($(this).parent().parent().attr("data-orderNumber"))
	})
	
	//取消订单
	$(".Cancelbtn").on("tap",function(){
		CancelOrder($(this).parent().parent().attr("data-orderNumber"))
		
	
	})

}